define(["../common"], function() {

var $form = $("#storeRestockForm");
var saveDoPath = g_shopDomain + "/storeRestock/addOrEditDo/json";

//使暴露在窗体的图片显示
$(window).scroll();

var formConfig = {
		fields: {
			"#srgAmount": {
				rule: "integer[+]",
				msg: {integer: "请添加进货商品"}
			}
		},
		valid: function(form) {
			if (!$form.showMsg("正在保存...", 'load', true)) return;
			$.post(saveDoPath, $(form).serialize(), function(data) {
				if (data.status !== 0) {
					$(form).showMsg(data.message, 'error');
					return;
				} 
				$(form).showMsg("保存成功", 'pass');
				setTimeout(function() {
					var isChecked = $("input[name='isChecked']:checked").val();
					location.href = g_shopDomain + "/storeRestock/list?isChecked=" + isChecked;
				}, 1000);
			});
		}
};

loadjs(["validator"], function() {$form.validator(formConfig);});

(function() {
	var restockId = $("[name='restockId']").val();
	if (restockId == "") {
		// 进入新增页面时隐藏进货单商品表单
		$("#zssp").hide();
	}
})();


// 事件：保存进货单
$("[id^='submitBt_']").click(function() {
	$("#zssp :checkbox:not(:checked)").parents("tr").remove();// 移除未勾选的
	$("#sgPage").remove(); // 移除分页栏
	countSrg();
	// 对进货商品相关表单进行重命名
	$("[id^='srgCh_']").each(function(i, el) {
		var sgId = ($(this).attr("id").split("_"))[1];
		$(this).attr("name", "srgses[" + i + "].id.storeGoodsId"); // id
		$("#restock_" + sgId).attr("name", "srgses[" + i + "].restock"); // 进货量
		$("#selfCost_" + sgId).attr("name", "srgses[" + i + "].selfCost"); // 进货价
		$("[name='type_" + sgId + "']").attr("name", "srgses[" + i + "].type"); // 入库方式
	});
	$form.trigger('validate');
});

// 事件：店铺商品查找
$("[id^='srgSearchBt_']").click(function() {
	searchStoreGoods(1);
});

// 事件： 翻页(查找店铺商品时)
$(document).on("click", "[id^='page_']", function() {
	var nowPage = ($(this).attr("id").split("_"))[1];
	searchStoreGoods(nowPage);
});

// 事件：删除进货商品
$(document).on("click", "[id^='delSrgBt_']", function(){
	$(this).parents("tr").remove();
});

// 店铺商品查找
function searchStoreGoods (nowPage) {
	if(!$("#srgSearchBt_1").showMsg("正在查询...", 'load', true)) return;
	$("#zssp :checkbox:not(:checked)").parents("tr").remove();// 移除未勾选的
	$("#sgPage").remove(); // 移除分页栏
	$.post(g_shopDomain + "/storeGoods/list/json",
	{
		searchWord: $("#srgSearchWord").val(),
		nowPage: nowPage,
		pageSize: 10
	},
	function(data) {
		if (data.status !== 0) {
			$("#srgSearchBt_1").showMsg(data.message, 'error');
			return;
		}
		var paging = data.result.paging;
		if (paging.totalResult === 0) {
			$("#srgSearchBt_1").showMsg("没找到商品", 'error');
			return;
		}
		$("#srgSearchBt_1").showMsg();
		var list = data.result.list;
		var $list = $("#zssp");
		var noAppend = true;
		var imgData;
		for (var i = 0; i < list.length; i++) {
			var srg = list[i];
			// 已经在列表中的就不必再追加显示,避免勾选重复
			if ($("#srgCh_" + srg.storeGoodsId).size() > 0) {
				continue;
			}
			noAppend = false;
			
			imgData = "";
			if (srg.pic) {
				imgData = "data='"+ srg.pic + "'";
			}
			var itemHtml = '' + 
				'<tr>' + 
				'	<td class="ch"><input type="checkbox" id="srgCh_' + srg.storeGoodsId + '" value="' + srg.storeGoodsId + '">&nbsp;</td>' +
				'	<td class="sp"><a href="javascript:;"><img src="' + g_resDomain + '/images/goods_def.jpg" ' + imgData + '>' + 
				'	<span>' + srg.barCode + '<br>' + srg.name + '</span></a></td>' +
				'	<td>' + srg.stock + srg.unit + '</td>' +
				'	<td><label><input name="type_' + srg.storeGoodsId + '" type="radio" value="0" data-rule="checked">损耗</label><label><input name="type_' + srg.storeGoodsId + '" type="radio" value="1">合并</label></td>' +
				'	<td><input type="text" class="k" id="restock_' + srg.storeGoodsId + '" value="" style="width:40px" data-rule="required;amount;gt[0];range[~9999]">' + srg.unit + '</td>' +
				'	<td><input type="text" class="k" id="selfCost_' + srg.storeGoodsId + '" value="" style="width:60px" data-rule="required;money;gt[0];range[~9999]">元</td>' +
				'	<td><a id="delSrgBt_' + srg.storeGoodsId + '_' + srg.name + '">删除</a></td>' +
				'</tr>';
			$list.append(itemHtml);
		}
		var pageCtrlHtml = '';
		if (paging.nowPage == 1) {
			pageCtrlHtml = '<div class="page" id="sgPage" style="padding-top:10px"><a href="javascript:;" id="page_' + (paging.nowPage + 1) + '">下一页</a></div>';
		} else if (paging.nowPage > 1 && paging.nowPage < paging.totalPage) {
			pageCtrlHtml = '<div class="page" id="sgPage" style="padding-top:10px"><a href="javascript:;" id="page_' + (paging.nowPage - 1) + '">上一页</a><a href="javascript:;" id="page_' + (paging.nowPage + 1) + '">下一页</a></div>';
		} else {
			pageCtrlHtml = '<div class="page" id="sgPage" style="padding-top:10px"><a href="javascript:;" id="page_' + (paging.nowPage - 1) + '">上一页</a></div>';
		}
		$list.after(pageCtrlHtml);
		// 当前页的查找结果都已经在已选列表中(会出现分页没有记录的现象),则自动加载下一页
		if (noAppend) {
			searchStoreGoods(paging.nowPage + 1);
			return;
		}
		// 显示进货单商品表单
		$("#zssp").show();
		// 使暴露在窗体的图片显示
		$(window).scroll();
	});
}

// 统计进货商品数
function countSrg() {
	var count = $("#zssp :checkbox:checked").length;
	$("#srgAmount").val(count).blur();
}


});